#include <gst/gst.h>
#include <stdio.h>

#define AUDIO_CAPS "application/x-rtp,media=audio, clock-rate=89000, width=16, height=16, encoding-name=L16, payload=96"

/*
  gst-launch-0.10 -v udpsrc port=5000 ! "application/x-rtp,media=audio, clock-rate=89000,
                  width=16, height=16, encoding-name=L16, payload=96" ! rtpL16depay !
                  audioconvert ! alsasink sync=false

  */
int main(int argc, char *argv[]){

    /*Main object*/
	GMainLoop *loop;

    /*Elements objects*/
    GstElement *pipeline,
               *source,
               *decoder,
               *convert,
               *sink;

    /*Cap object*/
    GstCaps *caps;

    /* Initialize GStreamer */
	gst_init (&argc, &argv);

	loop = g_main_loop_new (NULL, FALSE);

	/* Pipeline description*/
    /* gst-launch-0.10 -v udpsrc port=5000 caps="application/x-rtp,media=audio, clock-rate=89000, width=16, height=16, encoding-name=L16,
       payload=96" ! rtpL16depay ! audioconvert ! alsasink sync=false */

	/* Create gstreamer elements */
	pipeline = gst_pipeline_new ("audio-player");
    source   = gst_element_factory_make ("udpsrc","udpsrc");
    decoder  = gst_element_factory_make ("rtpL16depay","rtpL16depay");
    convert  = gst_element_factory_make ("audioconvert","audioconvert");
    sink     = gst_element_factory_make ("alsasink","alsasink");

    if (!pipeline || !source || !decoder || !convert || !sink) {
		g_printerr ("One element could not be created. Exiting.\n");
		return -1;
  	}
  	
    caps = gst_caps_from_string (AUDIO_CAPS);

  	g_object_set (G_OBJECT (source), "port", 5000, NULL);
  	g_object_set (G_OBJECT (source), "caps", caps , NULL);


    /* we add all elements into the pipeline */
    gst_bin_add_many (GST_BIN (pipeline),source, decoder, convert, sink, NULL);

	/* we link the elements together */	
    gst_element_link_many (source, decoder, convert, sink, NULL);

    /* Set the pipeline to "playing" state*/
	g_print ("Now playing...\n");
	gst_element_set_state (pipeline, GST_STATE_PLAYING);

	/* Iterate */
	g_print ("Running...\n");
	g_main_loop_run (loop);

    /* Stop playback */
    g_print ("Stopping playback\n");
	gst_element_set_state (pipeline, GST_STATE_NULL);

    /* Delete the pipeline */
	g_print ("Deleting pipeline\n");
	gst_object_unref (GST_OBJECT (pipeline));
	g_main_loop_unref (loop);

	return 0;

}
